package com.democxy.modules.cms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.List;

import com.democxy.common.baseData.ResponeData;
import com.democxy.common.utils.StringUtils;
import com.democxy.common.utils.html.HtmlStrUtil;
import com.democxy.modules.cms.entity.Blogs;
import com.democxy.modules.cms.service.BlogTypeService;
import com.democxy.modules.cms.service.BlogsService;


/**
 * 博客信息Controller
 * @author democxy
 * @version v1.0
 */
@RestController
@RequestMapping(value = "/admin/cms/blogs")
public class BlogsController{

	@Autowired
	private BlogsService blogsService;
	@Autowired
	private BlogTypeService blogTypeService;

	@RequestMapping(value = "get/{id}")
	public ResponeData<Blogs> get(@PathVariable("id") String id) {
		Blogs byId = blogsService.getById(id);
		return new ResponeData<Blogs>(byId);
	}
	
	@RequestMapping(value = "list")
	public ResponeData<PageInfo<Blogs>> list(@RequestBody Blogs blogs) {
		PageHelper.startPage(blogs.getPageNo(),15,"create_time desc");
		List<Blogs> list = blogsService.findList(blogs);
		PageInfo<Blogs> pageInfo = new PageInfo<Blogs>(list, 10);
		return new ResponeData<PageInfo<Blogs>>(pageInfo);
	}

	@RequestMapping(value = "save")
	public ResponeData<String> save(Blogs blogs) {
		if (StringUtils.isEmpty(blogs.getId())) {
			blogs.setLookNum(0);
			blogs.setCreateTime(new Date());
		}
		
		List<String> imgStr = HtmlStrUtil.getImgStr(blogs.getContent());
		if (imgStr.size()>0) {
			blogs.setMainPic(imgStr.get(0));
		}else {
			blogs.setMainPic("/default.jpg");
		}
		
		if (StringUtils.isEmpty(blogs.getSmallContent())) {
			String html2Text = HtmlStrUtil.Html2Text(blogs.getContent());
			if (html2Text.length()<200) {
				blogs.setSmallContent(html2Text);
			}else {
				blogs.setSmallContent(html2Text.substring(0, 200));
			}
			
		}
		
		blogsService.save(blogs);
		//存入缓存
		//redisUtil.set("blogs:"+blogs.getId(), JSON.toJSONString(blogs));
		
		return new ResponeData<String>("添加成功");
	}
	
	@RequestMapping(value = "delete")
	public ResponeData<String> delete(@RequestBody Blogs blogs) {
		blogsService.delete(blogs.getId());
		return new ResponeData<String>("删除成功");
	}

	@RequestMapping(value = "getBlogs")
	public ResponeData<List<Blogs>> getBlogs(@RequestParam("pageSize") Integer pageSize,
			@RequestParam("orderBy") String orderBy){
		PageHelper.startPage(1, pageSize, orderBy);
		List<Blogs> findList = blogsService.findList(new Blogs());
		PageInfo<Blogs> pageInfo = new PageInfo<Blogs>(findList);
		List<Blogs> list = pageInfo.getList();
		return new ResponeData<List<Blogs>>(list);
	}
}